package com.example.javaairportflightanalysis.service.impl;

import com.example.javaairportflightanalysis.comparator.DtimeComparator;
import com.example.javaairportflightanalysis.dao.InformationMapper;
import com.example.javaairportflightanalysis.domin.information.EventFlight;
import com.example.javaairportflightanalysis.domin.information.FlightNumberGroupDtime;
import com.example.javaairportflightanalysis.service.InformationService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class InformationServiceImpl implements InformationService {
    @Resource
    InformationMapper informationMapper;
    @Override
    public List<FlightNumberGroupDtime> selectFlightNumberA2() {
        List<FlightNumberGroupDtime> flightNumberGroupDtimesA2 = informationMapper.selectFlightNumberA2();
        List<FlightNumberGroupDtime> flightNumberGroupDtimeList = subDtime(flightNumberGroupDtimesA2);
        Collections.sort(flightNumberGroupDtimeList, new DtimeComparator<>());
        return flightNumberGroupDtimeList;
    }


    @Override
    public List<FlightNumberGroupDtime> selectFlightNumberA3() {
        List<FlightNumberGroupDtime> flightNumberGroupDtimesA3 = informationMapper.selectFlightNumberA3();
        List<FlightNumberGroupDtime> flightNumberGroupDtimeList = subDtime(flightNumberGroupDtimesA3);
        Collections.sort(flightNumberGroupDtimeList, new DtimeComparator<>());
        return flightNumberGroupDtimeList;
    }
    @Override
    public List<FlightNumberGroupDtime> selectFlightNumberD2() {
        List<FlightNumberGroupDtime> flightNumberGroupDtimesD2= informationMapper.selectFlightNumberD2();
        List<FlightNumberGroupDtime> flightNumberGroupDtimeList = subDtime(flightNumberGroupDtimesD2);
        Collections.sort(flightNumberGroupDtimeList, new DtimeComparator<>());
        return flightNumberGroupDtimeList;

    }
    @Override
    public List<FlightNumberGroupDtime> selectFlightNumberD3() {
        List<FlightNumberGroupDtime> flightNumberGroupDtimesD3 = informationMapper.selectFlightNumberD3();
        List<FlightNumberGroupDtime> flightNumberGroupDtimeList = subDtime(flightNumberGroupDtimesD3);
        Collections.sort(flightNumberGroupDtimeList, new DtimeComparator<>());
        return flightNumberGroupDtimeList;
    }

    @Override
    public List<EventFlight> selectEventFlightA2() {
        List<EventFlight> eventFlights = informationMapper.selectEventFlightA2();
        List<EventFlight> eventFlights1 = subEventFlightDtime(eventFlights);
        Collections.sort(eventFlights1, new DtimeComparator<>());
        return eventFlights1;
    }

    @Override
    public List<EventFlight> selectEventFlightD2() {
        List<EventFlight> eventFlights = informationMapper.selectEventFlightD2();
        List<EventFlight> eventFlights1 = subEventFlightDtime(eventFlights);
        Collections.sort(eventFlights1, new DtimeComparator<>());
        return eventFlights1;
    }

    @Override
    public List<EventFlight> selectEventFlightA3() {
        List<EventFlight> eventFlights = informationMapper.selectEventFlightA3();
        List<EventFlight> eventFlights1 = subEventFlightDtime(eventFlights);
        Collections.sort(eventFlights1, new DtimeComparator<>());
        return eventFlights1;
    }

    @Override
    public List<EventFlight> selectEventFlightD3() {
        List<EventFlight> eventFlights = informationMapper.selectEventFlightD3();
        List<EventFlight> eventFlights1 = subEventFlightDtime(eventFlights);
        Collections.sort(eventFlights1, new DtimeComparator<>());
        return eventFlights1;
    }


    private static List<FlightNumberGroupDtime> subDtime(List<FlightNumberGroupDtime> numberGroupDtimeList) {
        List<FlightNumberGroupDtime> subList = new ArrayList<>();
        Set<String> dtimeSet = new HashSet<>();

        for (FlightNumberGroupDtime flightNumberGroupDtime : numberGroupDtimeList) {
            String dtime = flightNumberGroupDtime.getDtime().substring(0, 11);
            if (!dtimeSet.contains(dtime)) {
                FlightNumberGroupDtime subFlightNumberGroupDtime = new FlightNumberGroupDtime();
                subFlightNumberGroupDtime.setDtime(dtime);
                subFlightNumberGroupDtime.setCount(flightNumberGroupDtime.getCount());
                // 根据需要截取其他属性并设置到subFlightNumberGroupDtime中

                subList.add(subFlightNumberGroupDtime);
                dtimeSet.add(dtime);
            }
        }

        return subList;
    }
    private static List<EventFlight> subEventFlightDtime(List<EventFlight> eventFlightList) {
        List<EventFlight> subList = new ArrayList<>();
        Set<String> dtimeSet = new HashSet<>();

        for (EventFlight eventFlight : eventFlightList) {
            String dtime = eventFlight.getDtime().substring(0, 11);
            if (!dtimeSet.contains(dtime)) {
                EventFlight newEventFlight = new EventFlight();
                newEventFlight.setDtime(dtime);
                newEventFlight.setAbstCount(eventFlight.getAbstCount());
                newEventFlight.setIarsCount(eventFlight.getIarsCount());
                newEventFlight.setTotalCount(eventFlight.getTotalCount());
                // 根据需要截取其他属性并设置到newEventFlight中

                subList.add(newEventFlight);
                dtimeSet.add(dtime);
            }
        }

        return subList;
    }
}
